package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.mapper.TbUserMapper;
import com.bwie.mapper.TbUserSexMapper;
import com.bwie.pojo.TbUser;
import com.bwie.pojo.UserSex;
import com.bwie.service.TbUserService;
import com.bwie.utils.MobileMessageUtil;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import jodd.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * description:
 *
 * @Author caoshuaifeng
 * @Create 2025/3/3 11:51
 * @Version 1.0
 */
@Service
public class TbUserServiceImpl implements TbUserService {

    @Autowired
    private TbUserMapper tbUserMapper;
    @Autowired
    TbUserSexMapper tbUserSexMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public R login(LoginInfoVo loginInfoVo) {
        TbUser tbUser = tbUserMapper.selectOne(new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserName, loginInfoVo.getUserName()));
        if (tbUser == null) {
            return R.ERROR(500,"用户不存在");
        }
        if (!tbUser.getUserPass().equals(loginInfoVo.getUserPass())) {
            return R.ERROR(500,"密码错误");
        }
        //生成token
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256,"123456").claim("userId", tbUser.getUserId()).claim("userName", tbUser.getUserName()).compact();
        //将token存入redis
        String tokenKey="token-"+tbUser.getUserId();
        stringRedisTemplate.opsForValue().set(tokenKey,token,10, TimeUnit.MINUTES);
        tbUser.setToken(token);
        return R.OK(tbUser);
    }

    @Override
    public R getUserById(Integer userId) {
        TbUser tbUser = tbUserMapper.selectById(userId);
        return R.OK(tbUser);
    }

    @Override
    public R getUserList(PageInfoVo pageInfoVo) {
        Page<TbUser> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(pageInfoVo.getKeyWord())){
           wrapper.like(TbUser::getUserName,pageInfoVo.getKeyWord())
                   .or().like(TbUser::getNickName,pageInfoVo.getKeyWord()).or()
                   .like(TbUser::getUserMobile,pageInfoVo.getKeyWord());
        }
        if(pageInfoVo.getTypeId()!=null ){
           wrapper.and(w -> w.eq(TbUser::getUserSex, pageInfoVo.getTypeId()));
        }
        Page<TbUser> tbUserPage = tbUserMapper.selectPage(page, wrapper);

        return R.OK(tbUserPage);
    }

    @Override
    public R getSexList() {
        List<UserSex> userSexList = tbUserSexMapper.selectList(null);
        return R.OK(userSexList);
    }

    @Override
    public R sendCode(String userMobile) {
        String code = RandomUtil.randomNumbers(4);
        System.out.println("验证码是"+code);
        MobileMessageUtil.sendMobileMessage(userMobile, code);
        stringRedisTemplate.opsForValue().set(userMobile,code,10, TimeUnit.MINUTES);
        return R.OK("发送成功");
    }

    @Override
    public R phoneLogin(LoginInfoVo loginInfoVo) {
        TbUser tbUser = tbUserMapper.selectOne(new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserMobile, loginInfoVo.getUserMobile()));
        TbUser tbUser1 = new TbUser();
        if (tbUser == null) {
            tbUser1.setUserMobile(  loginInfoVo.getUserMobile());
            tbUserMapper.insert(tbUser1);
        }
        String redisCode= stringRedisTemplate.opsForValue().get(loginInfoVo.getUserMobile());
        if(StringUtils.isEmpty(redisCode)){
            return R.ERROR(500,"验证码已过期");
        }
        if(!redisCode.equals(loginInfoVo.getUserCode())){
            return R.ERROR(500,"验证码错误");
        }

        return R.OK(tbUser1);
    }

    @Override
    public R updateStatus(Integer userId, Integer flag) {
        TbUser tbUser = tbUserMapper.selectById(userId);
        if(tbUser==null){
            return R.ERROR(500,"用户不存在");
        }
        tbUser.setUserStatus(flag);
        tbUserMapper.updateById(tbUser);
        return R.OK("修改成功");
    }

    @Override
    public R updateUser(TbUser tbUser) {
        tbUserMapper.updateById(tbUser);
        return R.OK();
    }

    @Override
    public R deleteById(Integer userId) {
        tbUserMapper.deleteById(userId);
        return R.OK();
    }
}
